package com.cms.web.queryAnalyse;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mrp.persistence.queryAnalyse.pageQuery.bean.EMPPageQuery;
import com.web.BaseController;
import com.cms.biz.business.MaintenanceDeliveryNoticeServiceMgr;
import com.cms.biz.queryAnalyse.CMSQueryAnalyseServiceMgr;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.dict.DMTServiceMgr;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.enums.EMPQueryParamEnum;

@Controller
@RequestMapping(value="/cmsWXCCTZQueryAnalyse")
public class EMPCMSWXCCQueryAnalyseController extends BaseController{
	@Autowired
	private                CMSQueryAnalyseServiceMgr                       queryAnalyseServiceMgr;

	@Autowired        
	private                            DMTServiceMgr                                DMTServiceMgr;

	@Autowired
	private      MaintenanceDeliveryNoticeServiceMgr                        deliveryNoticeService;
	
	@Autowired
	public HttpSession session;

	/**
	 * 生产单剩余数量
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/WXCCTZAnalyseQuery")
	public String WXCCTZQueryAnalyse(@RequestParam String F_QSRQ, @RequestParam String F_JZRQ, @RequestParam String F_HTBH, 
			                         @RequestParam String F_XMBH, @RequestParam String F_TXM, EMPPageQuery page, 
			                         @RequestParam String F_HTMC, @RequestParam String F_XMMC, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;
		String         BILL_WHERE = " WXCCTZXX.F_HTBH like '" + F_HTBH + "%' and WXCCTZXX.F_XMBH like '" + F_XMBH + "%' and "
		                          + " WXCCTZXX.F_ZDRQ >= '" + F_QSRQ + "' and WXCCTZXX.F_ZDRQ <= '" + F_JZRQ + "' and F_DJZT = '1'";
		
		if(!F_TXM.equals("")) {
			BILL_WHERE += " and WXCCTZXX.F_KJQJ = '" + F_TXM.substring(0, 6) + "' and WXCCTZXX.F_DJBH = '" + F_TXM.substring(6) + "'";
		}	
		
		po.SetValueByParamName("CTN_FCT1", "WXCCTZXX");
		po.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		po.SetValueByParamName("BILL_ORDER", "WXCCTZXX.F_KJQJ desc, WXCCTZXX.F_DJBH desc");
		po.setValue(EMPQueryParamEnum.PAGEQUERY, page);
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchCCTZ(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		page = (EMPPageQuery) queryRowSet.getExtProperty(EMPQueryParamEnum.PAGEQUERY, null);
		model.addAttribute("queryDataSet", queryDataSet);
		model.addAttribute("page", page);
		model.addAttribute("F_QSRQ", F_QSRQ);
		model.addAttribute("F_JZRQ", F_JZRQ);
		model.addAttribute("F_HTBH", F_HTBH);
		model.addAttribute("F_XMBH", F_XMBH);
		model.addAttribute("F_HTMC", F_HTMC);
		model.addAttribute("F_XMMC", F_XMMC);
		model.addAttribute("F_TXM", F_TXM);
		return "/cms/queryAnalyse/WXCCTZ/cctzQueryList";
	}
	
	/**
	 * 材料明细查询参数选择界面
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/preQueryForm/WXTZSYSLQueryAnalyseQueryPage")
	public String materialQueryPage(Model model) {
		String               F_YEAR = "";
		Calendar                cal = Calendar.getInstance();
		java.util.Date     currTime = new java.util.Date();
		SimpleDateFormat  formatter = new SimpleDateFormat("yyyyMM");
		F_YEAR = String.valueOf(cal.get(Calendar.YEAR));

		model.addAttribute("F_QSQJ", F_YEAR + "01");
		model.addAttribute("F_JZQJ", formatter.format(currTime));		
		return "/cms/queryAnalyse/preQueryForm/WXTZSYSLQueryPage";
	}
	
	/**
	 * 出厂内容查询
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/WXCCNRAnalyseQuery")
	public String WXCCNRAnalyseQuery(@RequestParam String F_XMBH, @RequestParam String F_XMMC, 
                                     @RequestParam String F_CPBH, @RequestParam String F_CPMC, EMPPageQuery page, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;

		po.SetValueByParamName("F_XMBH", F_XMBH);
		po.SetValueByParamName("F_CPBH", F_CPBH);
		po.setValue(EMPQueryParamEnum.PAGEQUERY, page);
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchCCNR(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		page = (EMPPageQuery) queryRowSet.getExtProperty(EMPQueryParamEnum.PAGEQUERY, null);
		model.addAttribute("queryDataSet", queryDataSet);
		model.addAttribute("page", page);
		model.addAttribute("F_XMBH", F_XMBH);
		model.addAttribute("F_XMMC", F_XMMC);
		model.addAttribute("F_CPBH", F_CPBH);
		model.addAttribute("F_CPMC", F_CPMC);
		return "/cms/queryAnalyse/WXCCTZ/ccnrList";
	}
	
	/**
	 * 生产内容查询
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/WXCCNRLCCCTZDAnalyseQuery")
	public String WXCCNRLCCCTZDAnalyseQuery(@RequestParam String F_XMBH, @RequestParam String F_CPBH, EMPPageQuery page, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;
		String         BILL_WHERE = "";

		BILL_WHERE = " exists(select 1 from WXCCTZXX where WXCCTZXX.F_KJQJ = WXCCTZMX.F_KJQJ and WXCCTZXX.F_DJBH = WXCCTZMX.F_DJBH and WXCCTZXX.F_DJZT = '1')" 
			       + " and exists(select 1 from FLOW_TASK_END_LIST where FLOW_TASK_END_LIST.BIZ_DATE = WXCCTZMX.F_KJQJ and " 
			       + " FLOW_TASK_END_LIST.BIZ_DJBH = WXCCTZMX.F_DJBH and FLOW_TASK_END_LIST.BIZ_GUID = WXCCTZMX.F_GUID and FLOW_TASK_END_LIST.FLOW_ID = 'WXCCTZ_FLOW')"
			       + " and WXCCTZMX.F_XMBH = '" + F_XMBH + "' and WXCCTZMX.F_CPBH = '" + F_CPBH + "'";
		po.SetValueByParamName("CTN_FCT1", "WXCCTZMX");
		po.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		po.SetValueByParamName("BILL_ORDER", "WXCCTZMX.F_KJQJ, WXCCTZMX.F_DJBH, WXCCTZMX.F_FLBH");
		po.setValue(EMPQueryParamEnum.PAGEQUERY, page);
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchCCTZ(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		page = (EMPPageQuery) queryRowSet.getExtProperty(EMPQueryParamEnum.PAGEQUERY, null);
		EFRowSet rowset = DMTServiceMgr.getRow("HTXMZD", F_XMBH);
		if(rowset != null) {
			model.addAttribute("F_XMMC", rowset.getString("F_XMMC", ""));
			rowset = null;
		}
		rowset = DMTServiceMgr.getRow("HTCPZD", F_CPBH);
		if(rowset != null) {
			model.addAttribute("F_CPMC", rowset.getString("F_CPMC", ""));
			rowset = null;
		}
		model.addAttribute("queryDataSet", queryDataSet);
		model.addAttribute("page", page);
		model.addAttribute("F_XMBH", F_XMBH);
		model.addAttribute("F_CPBH", F_CPBH);
		return "/cms/queryAnalyse/WXCCTZ/ccnrlccctzdList";
	}
	
	/**
	 * 评审合同编辑
	 * @param model
	 * @return
	 */
	@RequestMapping("/deliveryNotice/show/{F_GUID}")
	public String productNoticeEdit(@PathVariable("F_GUID") String F_GUID, Model model) {
		JParamObject           PO = JParamObject.Create();
		EFRowSet       nodeRowset = null;
		EFRowSet   cctzHeadRowSet = EFRowSet.getInstance();
		EFDataSet cctzItemDataSet = null;
		
		PO.SetValueByParamName("F_GUID", F_GUID);
		cctzHeadRowSet = deliveryNoticeService.loadEditWXCCTZForm(PO);
		
		cctzItemDataSet = cctzHeadRowSet.getDataSet("WXCCTZMX");
		
		model.addAttribute("dictObject", cctzHeadRowSet);
		model.addAttribute("dataset", cctzItemDataSet);			
		model.addAttribute("flowNodeObject", nodeRowset);
		return "/cms/queryAnalyse/WXCCTZ/form/form";
	}	
}
